import 'package:flutter/material.dart';
import 'package:xws/models/goods/goods_obj.dart';
import 'package:xws/utils/app_util.dart';
import 'package:xws/utils/theme_util.dart';
import 'package:xws/widgets/app_btn.dart';
import 'package:xws/widgets/app_img.dart';
import 'package:xws/widgets/app_list_tile.dart';

/// 赠送用户对象
class GoodsUser {
  GoodsUser({
    required this.name,
    required this.header,
  });

  final String name;
  final String header;
}

/// 送出确认弹框
class SendCheck extends StatefulWidget {
  const SendCheck({
    Key? key,
    required this.goodsObj,
    required this.user,
  }) : super(key: key);

  @override
  _SendCheckState createState() => _SendCheckState();

  final GoodsObj goodsObj;
  final GoodsUser user;
}

class _SendCheckState extends State<SendCheck> {
  GoodsObj get _g => widget.goodsObj;

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 300,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          const Text(
            '是否确认将物品',
            style: TextStyle(
              fontSize: 16,
              fontWeight: FontWeight.bold,
            ),
          ),
          const SizedBox(height: 16),
          _buildGoods,
          const SizedBox(height: 16),
          const Text(
            '赠予以下用户',
            style: TextStyle(
              fontSize: 16,
              fontWeight: FontWeight.bold,
            ),
          ),
          const SizedBox(height: 16),
          _buildUser,
          const SizedBox(height: 50),
          _buildBtns,
        ],
      ),
      padding: const EdgeInsets.all(20),
      decoration: BoxDecoration(
        color: white(context),
        borderRadius: BorderRadius.circular(10),
      ),
    );
  }

  Widget get _buildGoods {
    return Stack(
      children: <Widget>[
        AppImg.fitCover(
          height: 100,
          width: double.infinity,
          url: _g.photoUrlList.first,
          radius: 8,
        ),
        Container(
          height: 100,
          child: Text(
            _g.description,
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
            style: const TextStyle(
              color: Colors.white,
              fontSize: 14,
            ),
          ),
          alignment: Alignment.bottomLeft,
          padding: const EdgeInsets.all(10),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(8),
            gradient: LinearGradient(
              begin: Alignment.bottomCenter,
              end: Alignment.topCenter,
              colors: <Color>[
                Colors.black.withOpacity(0.8),
                Colors.black.withOpacity(0),
              ],
            ),
          ),
        ),
      ],
    );
  }

  Widget get _buildUser {
    return AppListTile(
      height: 30,
      leading: Padding(
        padding: const EdgeInsets.only(right: 10),
        child: AppImg.fitCover(
          height: 30,
          width: 30,
          url: widget.user.header,
          radius: 15,
        ),
      ),
      title: Text(
        widget.user.name,
        maxLines: 1,
        overflow: TextOverflow.ellipsis,
        style: const TextStyle(fontSize: 14),
      ),
    );
  }

  Widget get _buildBtns {
    return SizedBox(
      height: 50,
      child: Row(
        children: <Widget>[
          _btn('取消', false),
          const SizedBox(width: 20),
          _btn('确认', true),
        ],
      ),
    );
  }

  Widget _btn(
    String text,
    bool value,
  ) {
    return Flexible(
      child: AppBtn(
        onTap: () {
          close(context, value: value);
        },
        alignment: Alignment.center,
        radius: 8,
        text: text,
        textStyle: TextStyle(
          color: value ? theme(context) : red(context),
          fontSize: 16,
        ),
        color: (value ? theme(context) : red(context)).withOpacity(0.1),
      ),
    );
  }
}
